# Maintainer: Swarnava Ghosh <swarnavaghosh04@gmail.com>
# Credits: Maximilian Schiller <manimax3@outlook.de> [https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=krpc-cpp]

_realname=krpc-cpp
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=0.5.2
pkgrel=1
pkgdesc="KRPC Client for C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/krpc/krpc"
license=('spdx:LGPL-3.0-only AND spdx:GPL-3.0-or-later AND spdx:MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-asio"
  "${MINGW_PACKAGE_PREFIX}-protobuf")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/krpc/krpc/releases/download/v${pkgver}/krpc-cpp-${pkgver}.zip"
        "001-krpc-cpp-cmake.patch")
sha256sums=('4a1f5647dd96354f6d15eed169d81230bbcea2c8a60f94c918284531784ec9d3'
            '77e6028756bcfd06aa7ca6265327f9cf43ccade1ebcc657e826fea85d12aa0fd')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/001-$_realname-cmake.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -DPROTOBUF_USE_DLLS" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LESSER" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
